/**
 *
 */
package org.whuims.pois.action;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.whuims.pois.dao.KeywordDao;
import org.whuims.pois.hibernate.BaseDao;
import org.whuims.pois.model.Wordlist;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author herochen
 * @date 2010-4-20  @time 下午09:33:05
 */
public class KeywordAction extends DispatchAction {

    private KeywordDao dao = new KeywordDao();
    private List<Wordlist> list = new ArrayList<Wordlist>();
    static BaseDao baseDao = new BaseDao();

    public ActionForward show(ActionMapping mapping, ActionForm form,
                              HttpServletRequest request, HttpServletResponse response) {
        long topicId = getTopic(request);
        if (topicId <= 0) {
            return mapping.findForward("login");
        }
        list = dao.list(topicId);
        request.setAttribute("result", list);
        return mapping.findForward("show");
    }

    public ActionForward insert(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        Wordlist bean = (Wordlist) form;
        bean.setId(0);
        HttpSession session = request.getSession();
        long topicId = (Long) session.getAttribute("topicId");
        bean.setTopicid(topicId);
        String kwd = request.getParameter("keyword");
        if (kwd.trim().equals("")) {
            request.setAttribute("errormessage", "关键词不能为空");
            return mapping.findForward("error");
        }
        if (baseDao.findByParamsArr("from Wordlist where keyword=?", kwd).size() > 0) {
            request.setAttribute("errormessage", "关键词已经存在");
            return mapping.findForward("error");
        }
        dao.insert(bean);
        return mapping.findForward("success");
    }

    public ActionForward edit(ActionMapping mapping, ActionForm form,
                              HttpServletRequest request, HttpServletResponse response) {
        String idString = request.getParameter("id");
        Long id = Long.parseLong(idString);
        Wordlist bean = dao.select(id);
        request.setAttribute("bean", bean);
        return mapping.findForward("edit");
    }

    public ActionForward modify(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        Wordlist bean = (Wordlist) form;
        if (bean.getKeyword().trim().equals("")) {
            request.setAttribute("errormessage", "关键词不能为空");
            return mapping.findForward("error");
        }
        dao.modify(bean);
        return mapping.findForward("success");
    }

    public ActionForward delete(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {

        String idString = request.getParameter("id");
        Long id = Long.parseLong(idString);
        if (id == 0) {
            return mapping.findForward("success");
        }
        Wordlist bean = (Wordlist) baseDao.getByPk(Wordlist.class, id);
        dao.delete(bean);
        return mapping.findForward("success");
    }

    private long getTopic(HttpServletRequest request) {
        Object topic = request.getSession().getAttribute("topicId");
        if (topic == null) {
            return -1;
        }
        return (Long) topic;
    }
}
